$(function(){
	
	var URL = {
			dataGridUrl : ctx + "/background/companyInfo/grid",
			formWin : ctx + "/background/companyInfo/form",
			getInfoUrl : ctx + "/background/companyInfo/getInfo",
			deleteUrl : ctx + "/background/companyInfo/deleteById"
	};
	
	var modelName = "companyInfo";
	
	var grid = $("#"+modelName + "_table");
	
	var queryForm = $("#"+modelName+"_qf");
	
	renderGrid();
	bindEvents();
	bindSearchBtns();
	/** 渲染grid **/
	function renderGrid(){
		grid.datagrid({    
		    url:URL.dataGridUrl,
		    pagination:true,
		    pageNumber:1,
		    pageSize:20,
		    pageList:[10,20,30],
		    singleSelect:true,
		    showGroup: true,    
		    columns:[[
                {title:"id",field:"id",checkbox:true,width:50},
                {title:"联系人",field:"lxr",halign:"center",align:"left",width:120},
                {title:"联系电话",field:"lxdh",halign:"center",align:"left",width:120},
                {title:"联系邮箱",field:"lxyx",halign:"center",align:"left",width:120},
                {title:"详细地址",field:"xxdz",halign:"center",align:"left",width:220},
                {title:"经度",field:"jd",halign:"center",align:"left",width:80},
                {title:"纬度",field:"wd",halign:"center",align:"left",width:80},
                {title:"简介",field:"brief",halign:"center",align:"left",width:320},
                {title:"发布日期",field:"publishDate",halign:"center",align:"center",width:120},
                {title:"浏览量",field:"pv",halign:"center",align:"right",width:100},
                {title:"创建时间",field:"createTime",halign:"center",align:"center",width:160}
		    ]],
		    onDblClickRow:function(index, row){
		    	openFormWin(row.id,row.jd,row.wd);
		    },
		    onSelect:function(index, row){
		    	
		    },
		    onLoadSuccess:function(){

		    }
		});
		
	}
	/** 绑定事件 **/
	function bindEvents(){
		$("#"+modelName + "_addbtn").unbind().bind("click",function(){
			openFormWin();
		});
		$("#"+modelName + "_updatebtn").unbind().bind("click",function(){
			var row = getGridSelect(grid);
			if(row){
				openFormWin(row.id,row.jd,row.wd);
			}
		});
		$("#"+modelName + "_deletebtn").unbind().bind("click",function(){
			var ids = getGridChecked(grid);
			if(ids){
				$.messager.confirm("提示",Msg.deleteCfm,function(r){
					if(r){
						deleteById(ids);
					}
				});
			}
		});
	}
	
	/** 绑定查询按钮 **/
	function bindSearchBtns(){
		$("#"+modelName + "_qf_searchbtn").unbind().bind("click",function(){
			var formData = seriData(queryForm.serializeArray());
			grid.datagrid("load",formData);
		});
		$("#"+modelName + "_qf_clearbtn").unbind().bind("click",function(){
			queryForm.form("clear");
		});
	}
	
	/** 打开form页面 **/
	function openFormWin(id,jd,wd){
		var win = $("<div id='"+modelName+"_formWin'></div>").window({
						title: id?"简介[修改]":"简介[新增]",
						width:880,
						height:558,
						href : URL.formWin,
						closable:true,
						maximizable:false,
						minimizable:false,
						maximized:true,
						collapsible:false,
						resizable:false,
						onLoad : function(){
							destroyFormShade();
							$("#"+modelName+"_form_jd").val(jd);
							$("#"+modelName+"_form_wd").val(wd);
							$(function() {
								  var editor1 = KindEditor.create("#"+modelName+"_form_details",{
								  		pluginsPath:ctx +"/resources/js/plugs/kindeditor/plugins/",
									  	resizeType:1,
									  	width:"100%",
									  	height:"400px",
									  	uploadJson : ctx+"/uploadfile/fileupload",
									  	allowImageUpload: true, //多图上传
									  	dir : "image" ,
									  	filePostName : "file",
									  	afterChange:function(){
								        	this.sync();
								   		},
								   		afterBlur:function(){
								    		this.sync();
								   		}
							      });
								  
								  var editor2 = KindEditor.create("#"+modelName+"_form_fwcn",{
								  		pluginsPath:ctx +"/resources/js/plugs/kindeditor/plugins/",
									  	resizeType:1,
									  	width:"100%",
									  	height:"400px",
									  	uploadJson : ctx+"/uploadfile/fileupload",
									  	allowImageUpload: true, //多图上传
									  	dir : "image" ,
									  	filePostName : "file",
									  	afterChange:function(){
								        	this.sync();
								   		},
								   		afterBlur:function(){
								    		this.sync();
								   		}
							      });
							});
							if(id){
								//修改
								//加载信息
								formLoadData(id);
							}
						},
						onOpen:function(){
							showShade();
						},
						onBeforeDestroy:function(){
							destroyShade();
						},
						onClose : function(){
							win.window("destroy");
						}
					});
	}
	
	/** 表单加载数据 **/
	function formLoadData(id){
		$.ajax({
			url : URL.getInfoUrl,
			type : "post",
			data : {
				id : id
			},
			success : function(data){
				$("#"+modelName+"_form").form("load",data);
				KindEditor.html("#"+modelName+"_form_details",data.details);
				KindEditor.html("#"+modelName+"_form_fwcn",data.fwcn);
				$("#"+modelName+"_form_jd").change();
			}
		});
	}
	
	/** 根据id删除 **/
	function deleteById(id){
		$.ajax({
			url : URL.deleteUrl,
			type : "post",
			data : {
				id : id
			},
			success : function(data){
				if(data.success){
					showMsg(Msg.deleteSuc);
					grid.datagrid("reload");
				}
			}
		});
	}
	
});